#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>

using namespace std;

#define PRODUCTION_MAX 512
#define ITEM_MAX 128
#define VN_MAX 128
#define VT_MAX 128
#define STRING_MAX 1024

/**
 * used to index the sorted productions and items
 * @ptr:    means the subscript of production[] or item[]
 * @length: means the same type size
**/
struct Index
{
  int ptr;
  int size;
  Index (): ptr(-1), size(0) { }
};

struct Production
{
  int A;
  int B;
  int C;
};

struct Item
{
  int A;
  char B;
};

// index of Item by production.B and production.C
Index p_idx[VN_MAX][VN_MAX];

// index of Item by item.B
Index i_idx[VT_MAX];

Production production[PRODUCTION_MAX];

Item item[ITEM_MAX];

/**
 * dp[i][j][v] = n
 * There are n time of situation for string[i, j] to reduce to v
**/
unsigned int dp[STRING_MAX][STRING_MAX][VN_MAX];

int vn_num;
int production_num;
int item_num;
int string_length;
char str[STRING_MAX];

int main(int argc, char **argv)
{
  // basic io
  if (argc != 2)
  { printf ("cyk <input.txt>"); return 0; }

  freopen(argv[1], "r", stdin);
  scanf("%d\n", &vn_num); scanf("%d\n", &production_num);
  for (int i = 0; i < production_num; i++)
    scanf("<%d>::=<%d><%d>\n", &production[i].A, &production[i].B, &production[i].C);
  scanf("%d\n", &item_num);
  for (int i = 0; i < item_num; i++)
    scanf("<%d>::=%c\n", &item[i].A, &item[i].B);
  scanf("%d\n", &string_length); scanf("%s\n", str);

  // sort ascending order by item.B and item.A
  sort (item, item + item_num, [](const Item& a,
                                  const Item& b)
  { return a.B == b.B ? a.A < b.A : a.B < b.B; });

  // init the index which indexed by [item.B]
  for (int i = 0; i < item_num; i++)
  {
    int t = item[i].B;
    if (i_idx[t].ptr == -1) i_idx[t].ptr = i;
    i_idx[t].size++;
  }

  // sort ascending order by production.B, production.C and production.A
  sort (production, production + production_num, [](const Production& a,
                                                    const Production& b)
  { return a.B == b.B ? (a.C == b.C ? a.A < b.A : a.C < b.C) : a.B < b.B; });

  // init the index which indexed by [production.B][production.C]
  for (int i = 0; i < production_num; i++)
  {
    int n1 = production[i].B;
    int n2 = production[i].C;
    if (p_idx[n1][n2].ptr == -1) p_idx[n1][n2].ptr = i;
    p_idx[n1][n2].size++;
  }

  // compute len is 1 situation
  for (int i = 0; i < string_length; i++)
  {
    int t = str[i];
    for (int j = i_idx[t].ptr; j < i_idx[t].ptr + i_idx[t].size; j++)
      dp[i][i][item[j].A] = 1;
  }

  // compute len in [2, string_length] situation
  for (int len = 2; len <= string_length; len++)
  {
    for (int l = 0, r = l + len - 1; l <= string_length - len; l++, r++)
    {
      for (int mid = l + 1; mid <= r; mid++)
      {
        for (int i = 0; i < vn_num; i++)
        {
          for (int j = 0; j < vn_num; j++)
          {
            for (int k = p_idx[i][j].ptr; k < p_idx[i][j].ptr + p_idx[i][j].size; k++)
            {
              dp[l][r][production[k].A]
                += dp[l][mid - 1][i]
                * dp[mid][r][j];
            }
          }
        }
      }
    }
  }

  printf("%u\n", dp[0][string_length - 1][0]);
  return 0;
}
